package com.lhkj.ct.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.OperatorType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.meta.modules.psychology.model.dto.ProcessDto;
import com.lhkj.ct.meta.modules.psychology.model.dto.ScaleSubmitDto;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import com.lhkj.ct.meta.modules.questionnaire.model.entity.TblQnrAllotRecord;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrAllotRecordService;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("api/my/qnr/report")
@Api(tags = {"3.调查问卷"})
public class MyQuestionnaireController{

    @Resource
    private QnrAllotRecordService qnrAllotRecordService;

    @Resource
    private QnrAnswerService qnrAnswerService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "列表查询")
    @GetMapping("list")
    public ActionResult<IPage<TblQnrAllotRecord>> selectPatUnitPage(TblQnrAllotRecord record, PageQuery pageQuery) {
        record.setPatId(ShiroUtil.getAuthUserInfo().getUserId());
        return ActionResult.success(qnrAllotRecordService.selectAllotPage(record, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取问卷，题目，作答进度等信息", notes = "rId:报告id, info:问卷信息, questions:题目, answerJson:答案json, costTime:计时, anonymous:是否匿名")
    @GetMapping("{allotId}/get")
    @Log(title = "问卷作答", actionType = ActionType.QUERY, operatorType = OperatorType.USER)
    public ActionResult<Map<String, Object>> get(@PathVariable String allotId) {
        return ActionResult.success(qnrAllotRecordService.selectQnrWithQuestionsByAllotId(allotId, false));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看作答情况")
    @GetMapping("{allotId}/view")
    public ActionResult<ViewAnswerVo> view(@PathVariable String allotId) {
        return ActionResult.success(qnrAllotRecordService.viewAnswer(allotId));
    }

    @PostMapping("submit")
    @ApiOperation(value = "作答提交")
    @Log(title = "调查记录作答", actionType = ActionType.INSERT, operatorType = OperatorType.USER)
    public ActionResult<Void> submit(@RequestBody ScaleSubmitDto scaleSubmitDto) {
        qnrAnswerService.submitQuestionnaire(scaleSubmitDto);
        return ActionResult.success();
    }

    @PostMapping("process")
    @ApiOperation(value = "保存作答进度")
    public ActionResult<Void> saveProcess(@Validated @RequestBody ProcessDto process) {
        return ActionResult.judge(qnrAnswerService.saveProcess(process));
    }

}
